/**
 * Created by lenovo on 2017-4-12.
 */
require(['loading'], function(loading) {
    var params = getUrlParams();
    var pageSize = 20; // 每个页签用的每页显示记录条数大小
    var pageNo = 1; // 每个页签用的页码记录
    var noMore = false; // 没有更多内容啦标志
    var type = ''; 
    type = params.type?params.type:'';
   if(type==2){
   	$('.tabs span').removeClass('active');
   	$(".two").addClass('active');
   	
   }else if(type==3){
   	$('.tabs span').removeClass('active');
   	$(".three").addClass('active');
   }else if(type==4){
   	$('.tabs span').removeClass('active');
   	$(".four").addClass('active');
   }else if(type==5){
   	$('.tabs span').removeClass('active');
   	$(".five").addClass('active');
   }else if(type==6){
   	$('.tabs span').removeClass('active');
   	$(".six").addClass('active');
   }
    //tab切换
    $('.tabs span').on('click', function(){
        var ifActive = $(this).attr('class');
        if(ifActive == 'active'){
            return false;
        } else {
            $(this).addClass('active').siblings().removeClass('active');
            type = $(this).attr("type");
            allOrders.type = type;
            pageNo = 1;
            noMore = false;
            allOrders.moveShowOrders(false);
            if(type == 2){
                if(allOrders.waitReceiveOrders.length == 0){
                    allOrders.noData = true;
                }else{
                    allOrders.noData = false;
                }
            }else if(type == 3){
                if(allOrders.ReceiveOrders.length == 0){
                    allOrders.noData = true;
                }else{
                    allOrders.noData = false;
                }
            }else if(type == 4){
                if(allOrders.PayOrders.length == 0){
                    allOrders.noData = true;
                }else{
                    allOrders.noData = false;
                }
            }else if(type == 5){
                if(allOrders.Done.length == 0){
                    allOrders.noData = true;
                }else{
                    allOrders.noData = false;
                }
            }else if(type == 6){
                if(allOrders.qxDone.length == 0){
                    allOrders.noData = true;
                }else{
                    allOrders.noData = false;
                }
            }
            else{
                if(allOrders.allOrders.length == 0){
                    allOrders.noData = true;
                }else{
                    allOrders.noData = false;
                }
            }
        }
    });

    var allOrders = new Vue({
        el: '#allOrders',
        data: {
            showOrders:[],
            allOrders:[],
             waitReceiveOrders:[],
             ReceiveOrders:[],
             PayOrders:[],
              Done:[],
             qxDone:[],
            type: type,
            noData: false,
            showLoading: false,
            item: {
                params:{
                    pageSize:200, // 一下子从服务器端加载的页大小
                    pageNo:1, // 从服务器端获取订单的页码记录
                    orderStatusFlag:1,
                    token:token,
                    userToken: userToken
                }
            },
            orderListUrl:"http://321.sheyuan.com"+ '/v3.3/pforder/orderList'
        },
        ready: function() {
            this.getAllOrderList();
            console.log(333)
            bindScroll();
        },
        methods: {
            getAllOrderList:function(){
            	console.log(token)
                this.$http.get(this.orderListUrl, this.item)
                    .then(function(response) {
                        var result = JSON.parse(response.body);
                        console.log(result);
                        var allOrders = result.data.orderList.result;
                         console.log(allOrders);
                        if(allOrders.length == 0 && this.item.params.pageNo == 1){
                            this.noData = true;
                        }
                        //this.$set('allOrders', allOrders);
                        for (var orderIndex in allOrders) {
                            var statuz = allOrders[orderIndex].orderStatus;
                            if (statuz == 10 ) { // 待确认
                                this.waitReceiveOrders.push(allOrders[orderIndex]);
                            } else if (statuz == 20) { //已确认
                                this.ReceiveOrders.push(allOrders[orderIndex]);
                            }else if (statuz == 30) { //已付款
                                this.PayOrders.push(allOrders[orderIndex]);
                            } else if (statuz == 40|| statuz == 60 || statuz == 65 || statuz == 70) { //已完成
                                this.Done.push(allOrders[orderIndex]);
                            }  
                            else if (statuz == 50  ) {//已取消
                                this.qxDone.push(allOrders[orderIndex]);
                            }
                            this.allOrders.push(allOrders[orderIndex]);
                        }
                        this.moveShowOrders(true);
                    },function(response) {
                        //errorCallback
                    });
            },
            moveShowOrders :function(flag){
                var needTotalNum = pageSize * pageNo;
                var oldNeedTotalNum = needTotalNum;
                if (this.type == "2") {
                    if (this.waitReceiveOrders.length < needTotalNum && !flag) {
                        this.item.params.pageNo++;
                        this.getAllOrderList();
                    }
                    needTotalNum = this.waitReceiveOrders.length < needTotalNum?this.waitReceiveOrders.length:needTotalNum;
                    this.$set("showOrders",this.waitReceiveOrders.slice(0,needTotalNum));
                } else if (this.type == '3') {
                    if (this.ReceiveOrders.length < needTotalNum && !flag) {
                        this.item.params.pageNo++;
                        this.getAllOrderList();
                    }
                    needTotalNum = this.ReceiveOrders.length < needTotalNum?this.ReceiveOrders.length:needTotalNum;
                    this.$set("showOrders",this.ReceiveOrders.slice(0,needTotalNum));
                } else if (this.type == '4') {
                    if (this.PayOrders.length < needTotalNum && !flag) {
                        this.item.params.pageNo++;
                        this.getAllOrderList();
                    }
                    needTotalNum = this.PayOrders.length < needTotalNum?this.PayOrders.length:needTotalNum;
                    this.$set("showOrders",this.PayOrders.slice(0,needTotalNum));
                } 
                
                else if (this.type == '5') {
                    if (this.Done.length < needTotalNum && !flag) {
                        this.item.params.pageNo++;
                        this.getAllOrderList();
                    }
                    needTotalNum = this.Done.length < needTotalNum?this.Done.length:needTotalNum;
                    this.$set("showOrders",this.Done.slice(0,needTotalNum));
                } 
                else if (this.type == '6') {
                    if (this.qxDone.length < needTotalNum && !flag) {
                        this.item.params.pageNo++;
                        this.getAllOrderList();
                    }
                    needTotalNum = this.qxDone.length < needTotalNum?this.qxDone.length:needTotalNum;
                    this.$set("showOrders",this.qxDone.slice(0,needTotalNum));
                } 
                else {
                    if (this.allOrders.length < needTotalNum && !flag) {
                        this.item.params.pageNo++;
                        this.getAllOrderList();
                    }
                    needTotalNum = this.allOrders.length < needTotalNum?this.allOrders.length:needTotalNum;
                    this.$set("showOrders",this.allOrders.slice(0,needTotalNum));
                }
                noMore = oldNeedTotalNum > needTotalNum;
            }
        }
    });

    function bindScroll(){
        window.onscroll = function(){
            if($(window).scrollTop() + $(window).height() >= $(document).height()) {
                if (!noMore) {
                    pageNo++;
//					console.info("pageNo:" + pageNo);
                    allOrders.moveShowOrders(false);
                } else {
                    promptBox("没有更多内容啦！");
                }
            }
        }
    }
   
});




